package com.elcl.activity.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.elcl.andbaselibrary.R;
import com.elcl.activity.interfaces.BaseFragmentMethods;
import com.elcl.network.OkHttp;
import com.elcl.storage.ApplicationCache;
import com.elcl.util.BaseDialogUtils;

public abstract class BaseFragment extends Fragment implements BaseFragmentMethods {

    protected static final String B2T = "animation_btmToUp";
    protected static final String R2L = "animation_rightToLeft";


    @SuppressLint("HandlerLeak")
    protected Handler netHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == OkHttp.IS_NET_FAILED) {
                fail();
            } else if (msg.what == OkHttp.RECOGNISE_FAILED) {//认证失败
            } else {
                praseJsonMsg(msg);
            }
        }
    };

    private void praseJsonMsg(Message msg) {
        praseJson(msg.what, msg.obj.toString());
    }

    protected abstract void praseJson(int reqId, String json);

    protected void fail() {

    }


    protected Activity mAct;

    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mAct = getActivity();
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    /**
     * 界面中填充逐个填充textView的内容
     */
    @Override
    public void setText(View praentView, int viewId, String str) {
        TextView view = (TextView) praentView.findViewById(viewId);
        if (null != str) {
            view.setText(str);
        } else {
            view.setText("");
        }
    }

    @Override
    public void setListener(View praentView, int viewId,
                            OnClickListener clickListener) {
        View view = praentView.findViewById(viewId);
        view.setOnClickListener(clickListener);
    }

    /**
     * 设置控件的可视性，一般和setText连用
     */
    @Override
    public void setVisibility(View praentView, int viewId, int visibility) {
        View view = praentView.findViewById(viewId);
        view.setVisibility(visibility);
    }

    @Override
    public void showToast(String content) {
        Toast.makeText(ApplicationCache.context, content, Toast.LENGTH_SHORT)
                .show();
    }

    @Override
    public void showToast(int resId) {
        Toast.makeText(ApplicationCache.context, resId, Toast.LENGTH_SHORT)
                .show();
    }

    @Override
    public void showLongToast(String content) {
        Toast.makeText(ApplicationCache.context, content, Toast.LENGTH_LONG)
                .show();
    }

    @Override
    public void showLongToast(int resId) {
        Toast.makeText(ApplicationCache.context, resId, Toast.LENGTH_LONG)
                .show();
    }

    @Override
    public void openActivty(Class<?> cls) {
        startActivity(new Intent(getActivity(), cls));
    }

    @Override
    public void openActivty(Class<?> cls, String mid) {
        Intent intent = new Intent(ApplicationCache.context, cls);
        intent.putExtra("mid", mid);
        ApplicationCache.context.startActivity(intent);
    }

    public void openActivtyForResult(Class<?> cls, int resultId) {
        Intent intent = new Intent(ApplicationCache.context, cls);
        ((Activity) (ApplicationCache.context)).startActivityForResult(intent, resultId);
    }

    @Override
    public void openActivty(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(getActivity(), cls);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    public void openActivty(Class<?> cls, int[] animation) {
        Intent intent = new Intent(getActivity(), cls);
        startActivity(intent);
        getActivity().overridePendingTransition(animation[0], animation[1]);
    }

    @Override
    public void openActivtyByAnim(Class<?> cls, String animationType) {
        Intent intent = new Intent(getActivity(), cls);
        startActivity(intent);
        if (animationType.equals(B2T)) {
            getActivity().overridePendingTransition(R.anim.anim_btm2top,
                    R.anim.anim_still);
        } else if (animationType.equals(R2L)) {
            getActivity().overridePendingTransition(R.anim.anim_right_in,
                    R.anim.anim_still);
        }
    }

    @Override
    public void openActivtyByViewId(View view, int viewId, final Class<?> cls) {
        View objectiveView = view.findViewById(viewId);
        objectiveView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivty(cls);
            }
        });
    }

    @Override
    public void openActivtyByViewId(View view, int viewId, final Class<?> cls,
                                    final int[] animation) {
        View objectiveView = view.findViewById(viewId);
        objectiveView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivty(cls, animation);
            }
        });
    }

    @Override
    public void setText(View view, int[] viewId, String[] str) {
        int len = viewId.length;
        for (int i = 0; i < len; i++) {
            setText(view, viewId[i], str[i]);
        }
    }

    @Override
    public void setText(View view, int viewId, String str,
                        OnClickListener clickListener) {
        TextView objectiveView = (TextView) view.findViewById(viewId);
        objectiveView.setOnClickListener(clickListener);
        if (null != str) {
            objectiveView.setText(str);
        } else {
            objectiveView.setText("");
        }
    }

    @Override
    public void showProgressDialog(String... content) {
        BaseDialogUtils.showProgressDialog(content);
    }

    @Override
    public void dismissProgressDialog() {
        BaseDialogUtils.dismissProgressDialog();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            onFraVisible();
        } else {
            onFraInvisible();
        }
    }

    protected void onFraVisible() {
        lazyLoad();
    }

    /**
     * 懒加载（一般用在Viewpager中使用，防止每次都去加载所有的数据）
     */
    protected void lazyLoad() {
    }

    protected void onFraInvisible() {
    }

    @Override
    public void initBackBtn(int finishBtnId) {

    }

}
